package com.example.demo.service.impl;

import com.example.demo.entity.Coalconveyor;
import com.example.demo.entity.Personnelmanagearea;
import com.example.demo.handle.AreaListParseHandler;
import com.example.demo.handle.ConveyorListParseHandler;
import com.example.demo.mapper.CoalconveyorMapper;
import com.example.demo.service.CoalconveyorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-27
 */
@Service
public class CoalconveyorServiceImpl extends ServiceImpl<CoalconveyorMapper, Coalconveyor> implements CoalconveyorService {

    @Autowired
    private CoalconveyorMapper coalconveyorMapper;

    @Override
    public Integer insertConveyorData(String conveyorXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            ConveyorListParseHandler conveyorListParseHandler = new ConveyorListParseHandler();
            try {
                // 读取xml文件
                parser.parse(conveyorXml, conveyorListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + conveyorListParseHandler.getCoalconveyorArrayList().size() + "个节点被存储");
            for (Coalconveyor coalconveyor : conveyorListParseHandler.getCoalconveyorArrayList()) {
                System.out.println(coalconveyor.getCsMineCode());
                System.out.println(coalconveyor.getCsDataTime());
                System.out.println(coalconveyor.getOmWeighterCode());
                System.out.println(coalconveyor.getOmConveyequState());
                System.out.println(coalconveyor.getOmConveyequStarttime());
                System.out.println(coalconveyor.getOmConveyequStoptime());
                System.out.println(coalconveyor.getOmBeltRuntime());
                System.out.println(coalconveyor.getOmOutputHour());
                System.out.println(coalconveyor.getOmOutputDay());
                System.out.println(coalconveyor.getOmOutputMonth());
                System.out.println(coalconveyor.getOmOutputYear());
                System.out.println("----finish----");
                coalconveyorMapper.insertConveyorData(coalconveyor.getCsMineCode(),coalconveyor.getCsDataTime(),coalconveyor.getOmWeighterCode(),coalconveyor.getOmConveyequState(),coalconveyor.getOmConveyequStarttime(),coalconveyor.getOmConveyequStoptime(),coalconveyor.getOmBeltRuntime(),coalconveyor.getOmOutputHour(),coalconveyor.getOmOutputDay(),coalconveyor.getOmOutputMonth(),coalconveyor.getOmOutputYear());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
